package org.jzs.mybaseapp.section.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import org.jzs.mybaseapp.R;
import org.jzs.mybaseapp.common.utils.PermissionUtils;
import org.jzs.mybaseapp.common.utils.glide.GlideUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 动态权限
 */
public class PermissionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_camera, R.id.btn_phone, R.id.btn_location, R.id.btn_more, R.id.btn_setting})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_camera:
                PermissionUtils.getInstance().requestCamera(this);
                break;
            case R.id.btn_phone:
                PermissionUtils.getInstance().requestPhone(this);
                break;
            case R.id.btn_location:
                PermissionUtils.getInstance().requestLocation(this);
                break;
            case R.id.btn_more:
                PermissionUtils.getInstance().requestCustom(this);
                break;
            case R.id.btn_setting:
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
                break;
            default:

                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
    }

    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantedPermissions) {
            // 权限申请成功回调。
            switch (requestCode) {
                case PermissionUtils.REQUEST_CODE_PERMISSION_CAMERA:

                    break;
                case PermissionUtils.REQUEST_CODE_PERMISSION_PHONE:

                    break;
                case PermissionUtils.REQUEST_CODE_PERMISSION_LOCATION:

                    break;
                case PermissionUtils.REQUEST_CODE_PERMISSION_CUSTOM:

                    break;

                default:

                    break;
            }
            Toast.makeText(PermissionActivity.this, "权限已获得", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            // 权限申请失败回调。
//            switch (requestCode) {
//                case PermissionUtils.REQUEST_CODE_PERMISSION_CAMERA:
            //弹出提示框
            AndPermission.defaultSettingDialog(PermissionActivity.this, PermissionUtils.REQUEST_CODE_PERMISSION_CAMERA).show();
//                    break;
//
//                default:
//
//                    break;
//            }
        }
    };
}
